// 公用的请求
// #ifdef MP
const baseUrl = "https://meituan.thexxdd.cn/api/";
// #endif
// #ifndef MP
const baseUrl = "api/";
// #endif
import { Base64 } from "js-base64";

function getToken() {
  const token = uni.getStorageSync("userInfo").user_Token;
  // 加密
  const base64_token = Base64.encode(token + ":");
  // 空格
  return "Basic " + base64_token;
}
function http(url, method, data) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: baseUrl + url,
      method,
      data,
      header: { Authorization: getToken() },
      success: (res) => {
        console.log(res,'res');
        if (res.statusCode == 200) {
          resolve(res.data);
        } else if (res.statusCode == 401) {
          // 没有权限访问接口:跳转到登陆界面
          uni.navigateTo({ url: "/pages/login/login" });
          reject(res);
        } else if (res.statusCode == 400) {
          uni.showToast({
            title: "开发者某个字段或参数填写不对",
            icon: "none",
            duration: 1000,
          });
          reject(res);
        } else if (res.statusCode == 500) {
          uni.showToast({
            title: "服务器发生未知错误",
            icon: "none",
            duration: 1000,
          });
          reject(res);
        } else if (res.statusCode == 202) {
          uni.showToast({ title: res.data.msg, icon: "none", duration: 1000 });
          reject(res);
        } else {
          uni.showToast({
            title: "服务器发生未知错误",
            icon: "none",
            duration: 1000,
          });
          reject(res);
        }
      },
      fail: (err) => {
        uni.showToast({
          title: "服务器发生未知错误",
          icon: "none",
          duration: 1000,
        });
        reject(err);
      },
    });
  });
}
// 图片上传接口
let IMAGEURL = baseUrl + 'upload_picture';
// 身份证识别
let AICARD = baseUrl + 'ai_card';
export { http, IMAGEURL,AICARD };
